/*global pi */
pi.mod.define('pi.scene.movement', function (pi) {
	"use strict";

	var module = {},
		movement = [],
		interpIn2Pts = function (pt1, pt2, heightChecker, precision) { // 根据高度精度差值路径
			var path = [], path1, path2, midPt = {},
				d1 = heightChecker(pt1),
				d2 = heightChecker(pt2);

			if (Math.abs(d1.y - d2.y) > precision) {
				midPt = {
					x: (pt1.x + pt2.x) / 2,
					y: (pt1.y + pt2.y) / 2,
					z: (pt1.z + pt2.z) / 2
				};

				path1 = interpIn2Pts(pt1, midPt, heightChecker, precision);
				path2 = interpIn2Pts(midPt, pt2, heightChecker, precision);
				path = path.concat(path1);
				path.push(midPt);
				path = path.concat(path2);
			}

			return path;
		};

	/* 推动所有运动，如果返回值是 false 则表示运动完成，可以移除 */
	module.update = function (timeElapsed) {
		var i;
		for (i = 0; i < movement.length; i++) {
			if (!movement[i](timeElapsed)) {
				movement.splice(i, 1);
				i--;
			}
		}
	};

	/* 在指定场景对象上创建一个运动，运动逻辑依赖运动函数 fun */
	module.create = function (obj, fun) {
		var params = Array.prototype.slice.call(arguments, 2),
			updateFun = function (timeElapsed) {
				params.unshift(timeElapsed);
				params.unshift(obj);
				return fun.apply(undefined, params);
			};

		movement.push(updateFun);
		return updateFun;
	};

	/* 移除销毁一个运动，该运动由 create 创建出来 */
	module.del = function (mov) {
		var i = movement.indexOf(mov);
		if (i >= 0) {
			movement.splice(i, 1);
		}
	};

	/* 根据给定高度精度进行路径插值 */
	module.interpPath = function (path, heightChecker, heightPrecision) {
		var i, j, pt1, pt2, subPath, dstPath;

		dstPath = [].concat(path);
		// for (i = 0; i < dstPath.length - 1; i++) {
		// 	pt1 = dstPath[i];
		// 	pt2 = dstPath[i + 1];
		// 	subPath = interpIn2Pts(pt1, pt2, heightChecker, heightPrecision);
		// 	for (j = 0; j < subPath.length; j++) {
		// 		dstPath.splice(i, 0, subPath[j]);
		// 		i++;
		// 	}
		// }

		return dstPath;
	};

	/* 提供一个基于路径，并根据地表进行高度修正的运动函数 */
	module.createPathMovFun = function (path, nonCycle, heightChecker) {
		var time = 0;
		return function (obj, timeElapsed) {
			var r, pos;
			time += timeElapsed;
			r = pi.timeline.linear(["x", "y", "z"], path, 0, time, nonCycle);

			pos = {x: r.v[0], y: r.v[1], z: r.v[2]};
			obj.setPos(pos);
			if (heightChecker !== undefined) {
				obj.onGround();
			}

			return !r.over;
		};
	};

	/* 提供一个平滑旋转的运动函数 */
	module.createRotationFun = function (qFrom, qTo, totalTime) {
		var time = 0;
		return function (obj, timeElapsed) {
			var quat = {}, frac;
			time += timeElapsed;
			frac = time > totalTime ? 1 : time / totalTime;
			pi.math.quat.slerp(quat, qFrom, qTo, frac, true);
			obj.setOrient(quat);

			return time < totalTime;
		};
	};

	/* 提供一个播放动画序列的函数 */
	module.createAniShiftFun = function (aniParams) {
		var time = 0,
			totalTime = aniParams[aniParams.length - 1].time;

		if (aniParams[aniParams.length - 1].shiftTime !== undefined) {
			totalTime += aniParams[aniParams.length - 1].shiftTime;
		}

		return function (obj, timeElapsed) {
			var i, nextAni = obj.getNextAni();
			time += timeElapsed;

			for (i = 0; i < aniParams.length; i++) {
				if (time >= aniParams[i].time
						&& (i === aniParams.length - 1 || time < aniParams[i + 1].time)
						&& nextAni !== aniParams[i].ani) {
					obj.shiftAni(aniParams[i].ani, aniParams[i].shiftTime);
				}
			}

			return time < totalTime;
		};
	};

	return module;
});
